package com.building.hcrm.ui;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.building.hcrm.R;
import com.building.hcrm.base.BaseActivity;
import com.building.hcrm.base.BaseAsyncTask;
import com.building.hcrm.common.ComConst;
import com.building.hcrm.common.InStaffList;
import com.building.hcrm.common.SysMenuList;
import com.building.hcrm.common.User;
import com.building.hcrm.model.PlanDetail;
import com.building.hcrm.model.SysMenu;
import com.building.hcrm.ui.customer.CustomerActivityAudit;
import com.building.hcrm.ui.customer.CustomerMainActivity;

public class SpaceActivity extends BaseActivity
{
	private GridView gridView;
	private SimpleAdapter saImageItems;
	private String _zone;
	private ImageButton _imgBtnRe;
	private TextView _txtTitle;
	private ImageButton _imgBtnTitleRight;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO 自动生成的方法存根
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_space);
		_zone = getIntent().getStringExtra("Zone");
		InitTitle();
		gridView = (GridView) findViewById(R.id.gridView1);

		new AsyncTask<Object,Object,List<SysMenu>>()
		{
			private ProgressDialog pd;
			@Override
			protected void onPreExecute()
			{
				super.onPreExecute();
				if (null == SysMenuList.getInstance(SpaceActivity.this).getList()
						|| 0 == SysMenuList.getInstance(SpaceActivity.this).getList().size())
				{
					pd = new ProgressDialog(SpaceActivity.this);
					pd.setMessage("加载菜单...");
					pd.setIndeterminate(false);
					pd.setCancelable(false);
					pd.show();
				}
			}
			
			@Override
			protected List<SysMenu> doInBackground(Object... params)
			{
				return SysMenuList.getInstance(SpaceActivity.this).initList();
			}
			
			@Override
			protected void onPostExecute(List<SysMenu> result)
			{
				super.onPostExecute(result);
				if (null != pd && pd.isShowing())
					pd.cancel();
				setUi();
				gridView.setAdapter(saImageItems);

				// 添加消息处理
				gridView.setOnItemClickListener(new ItemClickListener());
				gridView.setOnTouchListener(new ItemTouchListener());
			}
		}.execute();
	}
	
	private void InitTitle()
	{
		_imgBtnRe = (ImageButton) findViewById(R.id.imgbtn_home);
		_imgBtnRe.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				finish();
			}
		});
		_txtTitle = (TextView) findViewById(R.id.titlePageName);
		if("Person".equals(_zone))
			_txtTitle.setText(R.string.space_title);
		else
			_txtTitle.setText(R.string.other_title);
		_imgBtnTitleRight = (ImageButton) findViewById(R.id.imgbtn_save);
		_imgBtnTitleRight.setVisibility(View.INVISIBLE);
	}
	
	private void setUi()
	{
		// 生成动态数组，并且转入数据
		ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();
		// 获取文字
		List<String> discText = new ArrayList<String>();
		discText = Arrays.asList(getResources().getStringArray(R.array.disc_menu));
		// 获取图片的顺序ID
		ArrayList<Integer> discID = new ArrayList<Integer>();
		
		for(Integer i = 0; i < discText.size(); i++)
		{
			String text = discText.get(i);
			if(null!= SysMenuList.getInstance(getApplicationContext()).getMenuName(text,_zone))
			{
				discID.add(i);
			}
		}
		
		for (Integer i = 0; i < discID.size(); i++)
		{
			HashMap<String, Object> map = new HashMap<String, Object>();

			map.put("ItemImage", R.drawable.main_item01 + discID.get(i));
			map.put("ItemText", SysMenuList.getInstance(SpaceActivity.this).getMenuName(discText.get(discID.get(i))));
			map.put("ItemTag", discText.get(discID.get(i)));
			lstImageItem.add(map);
		}
		// 生成适配器的ImageItem <====> 动态数组的元素，两者一一对应
		saImageItems = new SimpleAdapter(this, // 没什么解释
				lstImageItem,// 数据来源
				R.layout.item_main_menu,// night_item的XML实现

				// 动态数组与ImageItem对应的子项
				new String[] { "ItemImage", "ItemText" },

				// ImageItem的XML文件里面的一个ImageView,两个TextView ID
				new int[] { R.id.ItemImage, R.id.ItemText });

	}

	class ItemTouchListener implements OnTouchListener
	{
		@Override
		public boolean onTouch(View v, MotionEvent event)
		{
			v.clearFocus();
			return false;
		}
	}

	// 当AdapterView被单击(触摸屏或者键盘)，则返回的Item单击事件
	class ItemClickListener implements OnItemClickListener
	{
		@SuppressWarnings("unchecked")
		@Override
		public void onItemClick(AdapterView<?> arg0,// The AdapterView where the
													// click happened
				View arg1,// The view within the AdapterView that was clicked
				int arg2,// The position of the view in the adapter
				long arg3// The row id of the item that was clicked
		)
		{
			try
			{
				// 在本例中arg2=arg3
				HashMap<String, Object> item = (HashMap<String, Object>) arg0.getItemAtPosition(arg2);

				String[] discText = getResources().getStringArray(R.array.disc_menu);
				String tmp = item.get("ItemTag").toString();
				if (tmp.equals(discText[0]))//签到
				{
					Intent intent = null;
					intent = new Intent(getApplicationContext(), CheckInActivity.class);
					startActivity(intent);
				}
				else if (tmp.equals(discText[1]))//签到查询
				{
					Intent intent = null;
					intent = new Intent(getApplicationContext(), CheckInListActivity.class);
					startActivity(intent);
				}
				else if (tmp.equals(discText[2]))//客户管理
				{
					Intent intent = null;
					intent = new Intent(getApplicationContext(), CustomerMainActivity.class);
					startActivity(intent);
				}
				else if (tmp.equals(discText[3]))//客户审核
				{
					Intent intent = new Intent(getApplicationContext(),
							CustomerActivityAudit.class);
					startActivity(intent);
				}
				else if (tmp.equals(discText[4]))//任务制定
				{
					Intent intent = null;
					intent = new Intent(getApplicationContext(), TaskAddActivity.class);
					startActivity(intent);
				}
				else if (tmp.equals(discText[5]))//任务查询
				{
					Intent intent = null;
					intent = new Intent(getApplicationContext(), TaskLowListActivity.class);
					startActivity(intent);
				}
				else if (tmp.equals(discText[6]))//报销申请
				{

				}
				else if (tmp.equals(discText[7]))//报销审核
				{

				}
				else if (tmp.equals(discText[8]))//绩效考核
				{
					Intent intent = null;
					intent = new Intent(getApplicationContext(), KpiAddActivity.class);
					startActivity(intent);
				}
				else if (tmp.equals(discText[9]))//绩效查询
				{
					Intent intent = null;
					intent = new Intent(getApplicationContext(), KpiListActivity.class);
					startActivity(intent);
				}
				else if (tmp.equals(discText[10]))//市场调研
				{

				}
				else if (tmp.equals(discText[11]))//意见反馈
				{

				}
				else if (tmp.equals(discText[12]))//意见查询
				{

				}
				else if (tmp.equals(discText[13]))//按揭计算器
				{

				}
				else if (tmp.equals(discText[14]))//修改密码
				{
					Intent intent = null;
					intent = new Intent(getApplicationContext(), ChangePwdActivity.class);
					startActivity(intent);
				}
			}
			catch (Exception ex)
			{
				ex.printStackTrace();
			}
		}
	}
}
